//
//  HomeViewController.m
//  CarPlant
//
//  Created by bj_szd on 2025/3/24.
//

#import "CarNumberViewController.h"
#import "CarNumberCollectionViewCell.h"
#import "CarNumberHeaderView.h"

@interface CarNumberViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (weak, nonatomic) IBOutlet UILabel *carNumberInfoView;

@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, strong) NSDictionary *carNumberInfo;
@property (nonatomic, strong) NSArray *provinceArr;

@end

@implementation CarNumberViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *jsonInfoPath = [[NSBundle mainBundle] pathForResource:@"carNumberLicense.json" ofType:nil];
    NSData *json = [NSData dataWithContentsOfFile:jsonInfoPath];
    _carNumberInfo = [NSJSONSerialization JSONObjectWithData:json options:0 error:nil];
    _provinceArr = [_carNumberInfo allKeys];
    _provinceArr = [_provinceArr sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        return [obj1 localizedCompare:obj2];
    }];
    [self initCollectionView];
    NSLog(@"_provinceArr = %@",_provinceArr);
}

-(void)initCollectionView {
    [self.view addSubview:self.collectionView];
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    NSInteger count = _provinceArr.count;
    return count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    NSString *titleString = _provinceArr[section];
    NSArray *carNumberArr = [_carNumberInfo objectForKey:titleString];
    return carNumberArr.count;
}


- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSString *titleString = _provinceArr[indexPath.section];
    NSArray *carNumberArr = [_carNumberInfo objectForKey:titleString];
    NSString *resultString = carNumberArr[indexPath.item];
    CarNumberCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CarNumberCollectionViewCell" forIndexPath:indexPath];
    cell.carNumberView.text = resultString;
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
//    if (kind == UICollectionElementKindSectionHeader) {
//        CarNumberHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CarNumberHeaderView" forIndexPath:indexPath];
//        NSLog(@"[headerView class] = %@", [headerView class]);
        NSString *titleString = _provinceArr[indexPath.section];
//        headerView.titleView.text = titleString;
//        return headerView;
//    }
    UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"UICollectionReusableView" forIndexPath:indexPath];
    [[header subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
    UILabel *titleView = [[UILabel alloc] initWithFrame:CGRectMake(15, 40, kScreenWidth - 30, 60 - 40)];
    [header addSubview:titleView];
    titleView.text = titleString;
    titleView.textColor = [UIColor blackColor];
    titleView.font = [UIFont systemFontOfSize:15];
    return header;
}

#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake((kScreenWidth - 10) / 3, 60);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 5, 0, 5);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return CGSizeMake(kScreenWidth, 60);
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
    return CGSizeMake(0, 0);
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) collectionViewLayout:flowLayout];
        [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"UICollectionReusableView"];
        [_collectionView registerNib:[UINib nibWithNibName:@"CarNumberCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"CarNumberCollectionViewCell"];
        
        [_collectionView registerNib:[UINib nibWithNibName:@"CarNumberHeaderView" bundle:[NSBundle mainBundle]] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CarNumberHeaderView"];
        [self.view addSubview:_collectionView];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
    }
    return _collectionView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
